package com.leef.glidetest;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;

import com.bumptech.glide.DrawableRequestBuilder;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.leef.glidetest.bean.SecretData;
import com.leef.glidetest.common.Constant;
import com.leef.glidetest.utils.DecBufferedInputStream;
import com.leef.glidetest.utils.FileUtil;

import java.io.File;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.image)
    ImageView image;

    private SecretData data;
    private DrawableRequestBuilder<SecretData> builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        toolbar.setTitle("GLIDE");

        data = new SecretData();
        data.setsId("123");
        data.setPath(Constant.PATH_ENC);

        builder = Glide.with(getContext())
                .from(SecretData.class)
                .fitCenter().crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(R.mipmap.ic_launcher);
    }

    @OnClick(R.id.copy)
    void copy() {
        File file = new File(Constant.PATH_ENC);
        if (file.exists()) {
            System.out.println("already exists");
        } else {
            FileUtil.copyWithEncode(Constant.PATH_RAW, Constant.PATH_ENC, DecBufferedInputStream.VOR);
        }
    }

    @OnClick(R.id.show)
    void show() {
        System.out.println(this.data.getsId());
        builder.load(this.data).into(image);
    }

    private Context getContext() {
        return this;
    }
}
